package com.leadbank.bigdata.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

/**
 * 缺省Redis连接实现类。
 */
public class DefaultRedisConnection implements RedisConnection {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Jedis对象
     */
    private Jedis jedis;

    /**
     * Jedis池对象
     */
    private Pool<Jedis> pool;

    public DefaultRedisConnection(Pool<Jedis> pool) {
        this.pool = pool;
        jedis = this.pool.getResource();
    }

    public Jedis getJedis() {
        return jedis;
    }

    public void close() {
        try {
            pool.returnResource(jedis);
        } catch (Throwable e) {
            logger.error("Return jedis resource meet error.", e);
        }
    }

    public void closeBroken() {
        try {
            pool.returnBrokenResource(jedis);
        } catch (Throwable e) {
            logger.error("Return jedis resource meet error.", e);
        }
    }

    public void forceClose() {
        try {
            jedis.disconnect();
        } catch (Throwable e) {
        }
    }

}
